<?php
/**
 * Foreach
 *
 * This simply gives an easy way to iterate over arrays. 'foreach' works
 * only on arrays, and will issue an error when you try to use it on a
 * variable with a different data type or an uninitialized variable.
 *
 * @author  Badrus Said <badrus.said@gmail.com>
 * @since   gop1.0
 */
final class gop_Foreach extends gop_BaseIterator {
    public function execute(gop_Base $_caller = null) {
        $this->setCaller($_caller);
        $G1 = $this->getContext()->getCallee($this->_inp["G1"])->execute($this);
        $G2 = $this->getContext()->getCallee($this->_inp["G2"]);
        $G3 = $this->getContext()->getCallee($this->_inp["G3"]);
        if (!(($G2 instanceof Assignable) &&
              ($G3 instanceof Assignable))) {
            throw new Exception("ForeachException: G2 and G3 must be assignable.");
        }
        if (!is_array($G1)) {
            throw new Exception("ForeachException: G1 value must be array.");
        }
        foreach($G1 as $k => $v) {
            $G2->assign($k);
            $G3->assign($v);
            $G4 = $this->_inp["G4"];
            if (is_array($G4)) {
            foreach($G4 as $G) {
                $this->getContext()->getCallee($G)->execute($this);
                if ($this->_continue) continue 2;
                if ($this->_break) {
                    $this->interuptBreak();
                    break;
                }
                if ($this->_return) return;
            }} else {
                $this->getContext()->getCallee($G4)->execute($this);
                if ($this->_continue) continue;
                if ($this->_break) {
                    $this->interuptBreak();
                    break;
                }
                if ($this->_return) return;
            }
        }
        $this->chainExec();
    }
}
?>